<?php
session_start();
require_once __DIR__.'/../../utils/checkTokenUtils.php';
require_once __DIR__.'/../../model/BookCategoryModel.php';
use model\BookCategoryModel;

$errorMessage = $_SESSION['error_message'] ?? null;
$categoryId = isset($_GET['id']) ? intval($_GET['id']) : 0;

$categoryModel = new BookCategoryModel();
$category = $categoryModel->getCategoryById($categoryId); // 假设已添加getCategoryById方法

if (!$category) {
    $_SESSION['error_message'] = '分类不存在';
    header('Location: /categories/Categories.php');
    exit;
}

$title = '编辑分类';
$description = '修改现有图书分类信息';
ob_start();
?>

    <!-- 错误提示 -->
    <?php if ($errorMessage): ?>
        <div id="errorNotice" class="fixed top-20 left-1/2 transform -translate-x-1/2 bg-red-100 border border-red-400 text-red-700 px-4 py-2 rounded-lg shadow-lg max-w-md">
            <?= htmlspecialchars($errorMessage) ?>
        </div>
    <?php endif; ?>

    <!-- 清除已显示的消息 -->
    <?php
    unset($_SESSION['error_message']);
    ?>

    <!-- 编辑分类表单 -->
    <div class="container mt-8 max-w-xl mx-auto">
        <h1 class="text-2xl font-bold mb-4">编辑图书分类</h1>
        <form action="/process/edit_category.php" method="POST" class="bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4">
            <input type="hidden" name="id" value="<?= $category['id'] ?>">
            <div class="mb-4">
                <label class="block text-gray-700 text-sm font-bold mb-2" for="category_name">
                    分类名称
                </label>
                <input class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="category_name" name="category_name" type="text" value="<?= htmlspecialchars($category['category_name']) ?>" required>
            </div>
            <div class="flex items-center justify-between">
                <button type="submit" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline">
                    更新
                </button>
                <a href="/categories/Categories.php" class="text-blue-500 hover:text-blue-700">返回分类列表</a>
            </div>
        </form>
    </div>

<?php
$content = ob_get_clean();
require_once __DIR__.'/../layouts/Main.php';
?>